package org.example.secondkill.rabbitmq;

import org.example.secondkill.domain.Order;
import org.example.secondkill.redis.RedisService;
import org.example.secondkill.service.IGoodsService;
import org.example.secondkill.service.IKillService;
import org.example.secondkill.service.IOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MQReceiver {

    private static final Logger logger = LoggerFactory.getLogger(MQReceiver.class);
    public static int cnt = 0;
    @Autowired
    IGoodsService goodsService;
    @Autowired
    IOrderService orderService;
    @Autowired
    IKillService killService;

    @RabbitListener(queues = MQConfig.KILLSECOND_QUEUE)
    public void receive(String message) {
        OrderMessage mm = RedisService.stringToBean(message, OrderMessage.class);
        long userId = mm.getUserId();
        long goodsId = mm.getGoodsId();

        // 减库存 生成订单
        killService.placeAnOrder(userId, goodsId);
        cnt++;
        if (cnt % 1000 == 0) {
            logger.info("rabbit mq received: " + cnt);
        }
    }
}
